import redis

try:
    # 连接redis
    r = redis.Redis(host="localhost", port=6379, db=7)

    # 设置键值对
    r.set("test", "测试")

    # 获取键值
    value = r.get("test")
    print(value.decode('utf-8'))

except redis.exceptions.ConnectionError as e:
    print("连接错误：", e)
except redis.exceptions.AuthenticationError as e:
    print("认证错误：", e)
except Exception as e:
    print("其他错误：", e)
finally:
    # 关闭redis连接
    r.close()